<table class="table table-striped table-bordered table-hover">
    <?php if(empty($cartList)){echo "<div class='content' style='text-align: center; border-bottom: 1px solid white'><i>Hiện không có sản phẩm nào trong giỏ hàng</i></div>";} else{ ?>
    <thead>
    <tr>
        <th>#</th>
        <th>Tên sản phẩm</th>
        <th>Đơn giá</th>
        <th>Số lượng</th>
        <th>Thành tiền</th>
        <th></th>
    </tr>
    </thead>
    <tbody>
    <?php $i = 1; $total = 0; foreach($cartList as $objCartList){ $myArr = explode(",", $objCartList->images); ?>
    <tr>
        <td><?php echo $i ?></td>
        <td>
            <?php if($myArr[0] == '0'){ ?>
            <div class="col-lg-3">
                <img style="width: 150px; height: 100px" src="<?php echo base_url()?>assets/images/no_image.jpg">
            </div>
            <?php }else{ ?>
            <div class="col-lg-3">
                <img style="width: 150px; height: 100px" src="<?php  echo base_url()."uploads/".$myArr[0]; ?>">
            </div>
            <?php } ?>
            <div class="col-lg-9">
                <a target="_blank" href="<?php echo base_url() ?>product/view/<?php echo $objCartList->id ?>/title=<?php echo preg_replace('/\s+/', '+', $objCartList->product_name).'.html' ?>"><b><?php echo $objCartList->product_name ?></b></a>
            </div>
        </td>
        <td><?php echo number_format($objCartList->product_price1)." ".$objCartList->price_unit ?></td>
        <td><input id="inputNumberCartProduct<?php echo $i ?>" class="form-control" style="width: 40%" type="number" min="1" value="<?php echo $objCartList->quantity ?>" onchange="setUpdateValue('<?php echo $i ?>')"></td>
        <?php if($objCartList->price_unit != "USD"){ ?>
        <td><b><?php echo number_format(($objCartList->product_price1)*($objCartList->quantity)); $total += $objCartList->product_price1*$objCartList->quantity; ?> VND</b></td>
        <?php }else{ ?>
        <td><b><?php echo number_format(($objCartList->product_price1)*($objCartList->quantity)*($rate)); $total += $objCartList->product_price1*$objCartList->quantity*$rate; ?> VND</b></td>
        <?php } ?>
        <td>
            <div class="col-lg-6">
                <button id="btnCartProduct<?php echo $i ?>" class="btn btn-success disabled" role="button"><span class="glyphicon glyphicon-ok" onclick="updateProductCart('<?php echo $objCartList->id ?>', '<?php echo $objCartList->product_price1 ?>', '<?php echo $objCartList->price_unit ?>', '<?php echo $rate ?>', '<?php echo $objCartList->quantity ?>', '<?php echo $i ?>')"></span></button>
            </div>
            <div class="col-lg-6">
                <a class='btn btn-danger btn-xs pull-right' onclick="deleteProductCart('<?php echo $objCartList->id ?>', '<?php echo $objCartList->quantity ?>')"><span class='glyphicon glyphicon-remove'></span></a>
            </div>
        </td>
    </tr>
    <?php $i++; } ?>
    </tbody>
    <?php } ?>
</table>

<script>
    function setUpdateValue(i){
        var i = i;
        $('#btnCartProduct'+i).removeClass('disabled');
    }

    function updateProductCart(id, price, unit, rate, amount, i){
        var id = id;
        var price = price;
        var unit = unit;
        var rate = rate;
        var i = i;
        var oldQuantity = amount;
        var newQuantity = $('#inputNumberCartProduct'+i).val();
        if(newQuantity < 1){
            alert("Xin vui lòng nhập vào một số bất kỳ > 1");
        }
        else{
            var url = "<?php echo base_url() ?>";
            var dataString = "id="+id+"&oldQuantity="+oldQuantity+"&newQuantity="+newQuantity;
//        alert(dataString);

            $.ajax({
                type: "POST",
                url: url+"home/updateProductCart",
                data: dataString,
                success: function(x){
//                alert(x);
                    if(x != 'error'){
                        $.ajax({
                            url: url+"home/totalMoney/1",
                            success: function(y){
                                $('#spnShoppingCart').html(x);
                                $('#labelMoneyCartTable').html(y);
                                getTableCartManager(1);
                                getPage();
                            }
                        });
                    }
                    else{
                        alert("Đã xảy ra lỗi! Xin vui lòng làm mới trình duyệt và thử lại...");
                    }
                }
            });
        }
    }

    function deleteProductCart(id, quantity){
        //alert(id);
        var check = confirm("Bạn chắc chắn muốn xóa?");
        if(check == true){
            var id = id;
            var quantity = quantity;
            var url = "<?php echo base_url() ?>";
            var dataString = "id="+id+"&quantity="+quantity;
//            alert(dataString);

            $.ajax({
                type: "POST",
                url: url+"home/deleteProductCart",
                data: dataString,
                success: function(x){
                    if(x != 'error'){
                        $.ajax({
                            url: url+"home/totalMoney/1",
                            success: function(y){
                                $('#spnShoppingCart').html(x);
                                $('#labelMoneyCartTable').html(y);
                                getTableCartManager(1);
                                getPage();
                            }
                        });
                    }
                    else if(x == 0){
                        $('#spnShoppingCart').html(0);
                        $('#labelMoneyCartTable').html(0);
                        getTableCartManager(1);
                        getPage();
                    }
                    else{
                        alert("Đã xảy ra lỗi! Xin vui lòng làm mới trình duyệt và thử lại...");
                    }
                }
            });
        }
        else{
            return;
        }
    }
</script>

<script>
    function validate(evt) {
        var theEvent = evt || window.event;
        var key = theEvent.keyCode || theEvent.which;
        key = String.fromCharCode( key );
        var regex = /[0-9]|\./;
        if( !regex.test(key) ) {
            theEvent.returnValue = false;
            if(theEvent.preventDefault) theEvent.preventDefault();
        }
    }

    function moneyStringEdit(id, div){
        var n = parseInt($('#'+id).val().replace(/\D/g,''),10);
        var unit = $('#slEditUnit').val();
        if(isNaN(n)){
            n = 0;
        }
        $('#'+div).html(""+ n.toLocaleString() + " "+unit);
    }
</script>
